package com.atguigu.juc;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class AirConditioner {
    private int num = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void increment() {
        lock.lock();
        try {
            //1、判断
            while (num != 0) {
                condition.await();
            }
            //2、干活
            num++;
            System.out.println(Thread.currentThread().getName() + "\t" + num);
            //3、通知
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void decrement() {
        lock.lock();
        try {
            //1、判断
            while (num == 0) {
                condition.await();
            }
            //2、干活
            num--;
            System.out.println(Thread.currentThread().getName() + "\t" + num);
            //3、通知
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    //    public synchronized void increment() throws InterruptedException {
//        //1、判断
//        while (num != 0) {
//            this.wait();
//        }
//        //2、干活
//        num++;
//        System.out.println(Thread.currentThread().getName() + "\t" + num);
//        //3、通知
//        this.notifyAll();
//    }
//
//    public synchronized void decrement() throws InterruptedException {
//        while (num == 0) {
//            this.wait();
//        }
//        //2、干活
//        num--;
//        System.out.println(Thread.currentThread().getName() + "\t" + num);
//        //3、通知
//        this.notifyAll();
//    }

}

/**
 * 题目：现在两个线程，可以操作一个初始值为0的一个变量
 * 实现一个线程对该变量加1，另一个对该变量减1
 * 实现接替，来10轮，变量初始值为0
 * <p>
 * 1、 高内聚低耦合前提下，线程操作资源类
 * 2、 判断、干活、通知
 * 3、 多线程交互中，必须要防止多线程的虚假唤醒，也即（判断只用while，不能用if）
 * 4、 注意标志位的修改
 */
public class ThreadWaitNotifyDemo {

    public static void main(String[] args) {

        AirConditioner airConditioner = new AirConditioner();

        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    airConditioner.increment();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();


        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    airConditioner.decrement();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();


        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    airConditioner.increment();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "C").start();


        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    airConditioner.decrement();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "D").start();

    }

}

